#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections.Specialized;
using Gizmox.WebGUI.Common.Resources;
using System.IO;
using TVC.Report;
using FlexCel.Core;
using Gizmox.WebGUI.Common.Gateways;
using TVC.Modules.CustomControl;

#endregion

namespace TVC.Forms
{
    public partial class frmTransferOut : Form
    {
        string _strType = "";
        public MainForm MainForm { get; set; }
        public frmTransferOut(string strType, MainForm form)
        {
            InitializeComponent();
            _strType = strType;
            MainForm = form;
        }

        public frmTransferOut(string _strType, object dt, MainForm form)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            this._strType = _strType;

            if (dt is DataTable)
            {

                dgvList.DataSource = dt;
            }
            MainForm = form;
        }


        private void btnExport_Click(object sender, EventArgs e)
        {
            if (rdoXMLFile.Checked == true)
            {
                ExportToXml();
            }
            else
            {


                ExportToExcel();
            }
            //this.Close();
        }

        public void ExportToExcel()
        {
            try
            {
                DataTable dt = dgvList.DataSource as DataTable;
                DataTable dtnew = dt.Copy();
                DataSet DS = new DataSet(_strType);
                dtnew.TableName = _strType;
                DS.Tables.Add(dtnew);
                //DataTable dt_param = new DataTable();
                //dt_param.TableName = "params";
                DataTable dt_param = new DataTable("params");
                dt_param.Columns.Add("Code", typeof(string));
                dt_param.Columns.Add("Description", typeof(string));
                dt_param.Columns.Add("ValueFrom", typeof(string));
                dt_param.Columns.Add("ValueTo", typeof(string));
                dt_param.Columns.Add("IsNot", typeof(string));
                dt_param.Columns.Add("Operate", typeof(string));

                DataTable dt_filter = new DataTable();
                dt_filter.TableName = "FilterPara";
                DS.Tables.Add(dt_filter);
                DS.Tables.Add(dt_param);
                ReportGenerator _report = new ReportGenerator(DS, _strType, "", MainForm.AppConnectString, MainForm.ServerPath + "ReportTemplate\\", MainForm.ServerPath + "Report\\", "");
                _report.UserID = MainForm.PODInfo.USER_ID;
                _report.ValueList.Add("BUID", MainForm.PODInfo.Business_ID);
                ExcelFile exfile = _report.CreateReport();
                ExcelFile file = exfile;
                MemoryStream ms = new MemoryStream();
                file.Save(ms);

                DownloadGateway myDownload = new DownloadGateway(ms, _report.QdCode + ".xls");

                myDownload.ContentType = "application/vnd.ms-excel";
                myDownload.StartDownload(this);
            }
            catch (Exception ex)
            {
                TVC.MainForm.AddLog("Error", string.Format("{0}:({1}@{2}):{3}:{4}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), MainForm.PODInfo.USER_ID, MainForm.DB, "ExportToExcel", ex.Message));
                throw ex;
            }
        }

        public void ExportToXml()
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                //string filename = Context.HttpContext.Request.MapPath("/") + "\\" + _strType + ".xml";
                DataTable dt = dgvList.DataSource as DataTable;
               
                DataTable dtS = dt.Copy();
                dtS.TableName = this._strType;
                dtS.WriteXml(ms);
                FileDownloadGateway downloadGateway = new FileDownloadGateway();
                downloadGateway.Filename = _strType + ".xml";
                //      downloadGateway.ver = file.Version;
                downloadGateway.SetContentType(DownloadContentType.OctetStream);
                downloadGateway.StartBytesDownload(new ContainerControl(), ms.ToArray());

                //DownloadGateway myDownload = new DownloadGateway(ms, _strType + ".xml");

                //myDownload.ContentType = "application/xml";
                //myDownload.StartDownload(this);
            }
            catch (Exception ex)
            {
                TVC.MainForm.AddLog("Error", string.Format("{0}:({1}@{2}):{3}:{4}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), MainForm.PODInfo.USER_ID, MainForm.DB, "ExportToXml", ex.Message));
                throw ex;
                
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            DataTable dt = dgvList.DataSource as DataTable;
            if (txtTo.Text == "")
                dt.DefaultView.RowFilter = String.Format("{0} = '{1}'", dt.PrimaryKey[0].ColumnName, txtFrom.Text);
            else
            {
                dt.DefaultView.RowFilter = String.Format("{0} >= '{1}' and {0} <= '{2}'", dt.PrimaryKey[0].ColumnName, txtFrom.Text, txtTo.Text);
            }
        }

        private void frmTransferOut_Load(object sender, EventArgs e)
        {
            if (dgvList.Rows.Count == 0)
            {
                if (!MainForm.IsContainsTables(_strType))
                    MainForm.LoadDataTable(_strType);
                dgvList.DataSource = MainForm.GetTable(_strType).Copy();
            }
        }


    }
}